#ifndef _ALGOPERATE_H_
#define _ALGOPERATE_H_

#include"AlgComInfo.h"
#include"UserOperate.h"


#ifdef _WIN32_PLATFORM_
#include<Windows.h>
typedef HMODULE MODULE_HANDLE;
#endif

#ifdef _LINUX_PLATFORM_
#include<dlfcn.h>
typedef void * MODULE_HANDLE;
#endif



MODULE_HANDLE dll_Open(const char *dllpath)
{
#ifdef _WIN32_PLATFORM_
	return LoadLibraryA(dllpath);
#endif

#ifdef _LINUX_PLATFORM_
	return dlopen(dllpath, RTLD_NOW|RTLD_GLOBAL);
#endif
}

void dll_Close(MODULE_HANDLE h)
{
	if(h)
	{
#ifdef _WIN32_PLATFORM_
	FreeLibrary(h);
#endif

#ifdef _LINUX_PLATFORM_
	dlclose (h);
#endif
	}
}

void *dll_GetProc(MODULE_HANDLE h, const char *funname)
{
	if(h)
	{
#ifdef _WIN32_PLATFORM_
	return (void *)GetProcAddress(h, funname);
#endif

#ifdef _LINUX_PLATFORM_
	return dlsym(h,funname);
#endif
	}
	return NULL;
}


/*
void dll_GetLastErrorMsg(char *p, int size)  
{  
#if defined(_WIN32_PLATFORM_)  
    printf(p,size, "dll error(%u)",::GetLastError());  
#endif  
  
#if defined(_LINUX_PLATFORM_)  
    printf(p,size, "%s",dlerror());  
#endif  
} 
*/



class AlgOperate
{
	MODULE_HANDLE m_hModule;
public:
	AlgOperate()
	{
		m_hModule = NULL;
	}
	~AlgOperate()
	{
		dll_Close(m_hModule);
	}

	MODULE_HANDLE alg_Open(const char *dllpath);
	//void dll_Close(MODULE_HANDLE h);
	//void *dll_GetProc(MODULE_HANDLE h, const char *funname);


	bool AlgLoad(AlgComInfo algcominfo);
	bool AlgCall(unsigned int keyid, string key, AlgComInfo algcominfo, char *inBuffer, char *outBuffer, unsigned int inBufferSize, unsigned int outBufferSize);
};

typedef void (*DllFunc)(unsigned int, string, AlgComInfo, char *, char *, unsigned int, unsigned int);

#endif

